package com.mrent.web.controller.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mrent.entity.AdminPayment;
import com.mrent.entity.ManagerPayment;
import com.mrent.entity.OrderManager;
import com.mrent.service.AdminPaymentService;
import com.mrent.service.ManagerPaymentService;
import com.mrent.service.Page;
import com.mrent.util.Constance;
import com.mrent.util.RenderUtil;

@Controller
public class ManagerPaymentController {
	
	@Autowired
	private ManagerPaymentService managerPaymentService;
	
	@Autowired
	private AdminPaymentService adminPaymentService;
	
	/**
	 * 支付管理.
	 * @return
	 */
	@RequestMapping("/manager/payment/index")
	public String index(){
		
		return "manager/payment/home";
	}
	
	/**
	 * 支付列表.
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/payment/list")
	public String list(String page,HttpSession session,HttpServletResponse response){
		int p = 1;
		Page pp = new Page();
		try{
			p = Integer.parseInt(page);
		}catch(Exception e){
			p = 1;
		}
		pp.setPageNo(p);
		pp.setPageSize(Constance.DEFAULT_PAGE_SIZE);
		OrderManager om = (OrderManager)session.getAttribute(Constance.SESSION_MANAGER_NAME);
		List<ManagerPayment> payments ;
		if(om != null){
			payments = managerPaymentService.listPayment(om.getOrderMember(), pp);
			Map<String,Object> json = new HashMap<String,Object>();
			json.put("rows", payments);
			json.put("total", pp.getTotalCount());
			RenderUtil.renderJson(json,response);
		}
		return null;
	}
	
	/**
	 * 店主添加支付方式.
	 * 
	 * @param payId
	 * @param model
	 * @return
	 */
	@RequestMapping("/manager/payment/add/{payId}")
	public String add(@PathVariable Long payId,ModelMap model){
		model.addAttribute("payId", payId);
		return "manager/payment/edit";
	}
	
	/**
	 * 店店保存支付方式.
	 * 
	 * @param payId
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/payment/save")
	public String save(Long payId,String account,HttpServletResponse response,HttpSession session){
		OrderManager om = (OrderManager)session.getAttribute(Constance.SESSION_MANAGER_NAME);
		Map<String,Object> json = new HashMap<String,Object>();
		if(om != null){
			ManagerPayment managerPayment = new ManagerPayment();
			AdminPayment adminPayment = adminPaymentService.getPayment(payId);
			if(adminPayment != null){
				managerPayment.setPayId(adminPayment.getPayId());
				managerPayment.setPayDesc(adminPayment.getPayDesc());
				managerPayment.setPayFee(adminPayment.getPayFee());
				managerPayment.setPayName(adminPayment.getPayName());
				managerPayment.setPayUrl(adminPayment.getPayUrl());
				managerPayment.setAccount(account);
			}else{
				
			}
			managerPayment.setManager(om.getOrderMember());
			managerPaymentService.savePayment(managerPayment);
		}
		
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		return null;
	}
	
	/**
	 * 店主删除他的支付方式.
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/manager/payment/del/{id}")
	public String del(@PathVariable Long id,HttpServletResponse response){
		Map<String,Object> json = new HashMap<String,Object>();
		managerPaymentService.delPayment(id);
		json.put("result", "Success");
		RenderUtil.renderJson(json, response);
		
		return null;
	}

}
